// store/modules/grades.js
import Mock from 'mockjs';

const gradesModule = {
  state: () => ({
    allGrades: []
  }),
  mutations: {
    // 设置所有成绩
    SET_ALL_GRADES(state, grades) {
      state.allGrades = grades;
    },
    
    // 更新单个成绩
    UPDATE_GRADE(state, updatedGrade) {
      const index = state.allGrades.findIndex(grade => 
        grade.studentId === updatedGrade.studentId &&
        grade.course === updatedGrade.course &&
        grade.semester === updatedGrade.semester
      );
      
      if (index !== -1) {
        state.allGrades[index] = { ...state.allGrades[index], ...updatedGrade };
      }
    }
  },
  actions: {
    // 获取所有成绩
    async fetchAllGrades({ commit }) {
      // 模拟API请求，生成随机成绩数据
      const mockData = Mock.mock({
        'data|50-100': [
          {
            'studentId|100000-999999': 0,
            'name': '@cname',
            'course': '@pick(["数学", "语文", "英语", "物理", "化学", "生物", "历史", "地理", "政治"])',
            'semester': '@pick(["2023-2024-1", "2023-2024-2", "2024-2025-1", "2024-2025-2"])',
            'score|0-100': 0,
            'comment': '@sentence(5, 10)'
          }
        ]
      }).data;
      
      commit('SET_ALL_GRADES', mockData);
    }
  },
  getters: {
    // 获取所有成绩
    allGrades: state => state.allGrades
  }
};

export default gradesModule;